﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Flame.Dlr;
using Flame.Reflection;

namespace Flame.Controls.Common
{
    public abstract class AAutoComplete : IAutocomplete
    {
        //Dictionary<Type, IIntellisense> _intellisenseMap = new Dictionary<Type, IIntellisense>();
        ICallTip _calltip;
        IExecEngines _compilerManager;

        protected abstract bool show(Intellisense.Result r);
        protected abstract string textToCaret { get; }
        public abstract bool IsActive { get; }

        public AAutoComplete(IExecEngines compilerManager, ICallTip calltip)
        {
            _compilerManager = compilerManager;
            _calltip = calltip;
            /*Type t = Languages.Get("IronRubyExec");
            var exec = compilerManager.GetEngine(t);
            _intellisenseMap.Add(t, new RubyIntellisense(exec));
            t = Languages.Get("IronPythonExec");
            exec = compilerManager.GetEngine(t);
            _intellisenseMap.Add(t, new PythonIntellisense(exec));*/
        }

        private bool showAutocompleteNames(string text) { return show(Intellisense.Instance.GetListOfNames(text, _currentIntellisenseEngine)); }

        Type _currentLanguage;
        public Type CurrentLanguage { set { _currentLanguage = value; } }

        IIntellisense _currentIntellisenseEngine
        {
            get
            {
                return _compilerManager.GetIntellisense(_currentLanguage);
            }
        }

        bool showInfoTips(string textOfMethod)
        {
            _calltip.IOverloadList.Clear();
            Intellisense.Result r = Intellisense.Instance.GetListMethodsInfo(textOfMethod, _currentIntellisenseEngine);
            if (r != null)
            {
                foreach (IntellisenseItem mi in r.List)
                    _calltip.IOverloadList.Add(mi.Name);

                _calltip.ShowOverload();
                return true;
            }
            return false;
        }

        public void TryShow()
        {

            //ste hack

            /*if (false && _currentIntellisenseEngine is PythonIntellisense)
            {
                var py = _currentIntellisenseEngine as PythonIntellisense;

                string s1, s2;
                IntellisenseHelper.Istance.GetMatchResults(textToCaret, out s1, out s2);

                if (s1 != "" && s2 == "")
                {
                }


                IntellisenseHelper.Result r = new IntellisenseHelper.Result();


                r.PartialLength = s2.Length;
                r.StartingWithCount = s2.Length;

                r.List = (from x in py.GetList(s1) where x.ToLower().Contains(s2.ToLower()) select new IntellisenseItem() { Name = x, Type = IntellisenseItemType.None }).ToList();
                r.List.Sort();
                //r.List = py.GetList(s1).ToList();
                show(r);

                return;
            }*/

            if (_currentIntellisenseEngine == null) return;
            string text = textToCaret;
            if (!showAutocomplete(text))
            {
                if (!showInfoTips(text))
                {
                    showAutocompleteNames(text);
                }
            }


            /*if (false && _currentIntellisenseEngine is PythonIntellisense)
            {
                var py = _currentIntellisenseEngine as PythonIntellisense;

                string s1, s2;
                IntellisenseHelper.Istance.GetMatchResults(textToCaret, out s1, out s2);

                if (s1 != "" && s2 == "")
                {
                }


                IntellisenseHelper.Result r = new IntellisenseHelper.Result();


                r.PartialLength = s2.Length;
                r.StartingWithCount = s2.Length;

                r.List = r.List.Concat<IntellisenseItem>((from x in py.GetList(s1) where x.ToLower().Contains(s2.ToLower()) select new IntellisenseItem() { Name = x, Type = IntellisenseItemType.None }).ToList()).ToList();
                r.List.Sort();
                //r.List = py.GetList(s1).ToList();
                show(r);

                return;
            }*/

        }

        bool showAutocomplete(string text)
        {

            return show(Intellisense.Instance.GetListOfMembers(text, _currentIntellisenseEngine));
        }
    }
}
